package com.best4cloud.user.web.rest.request;

import com.best4cloud.common.core.security.SecurityUtils;
import com.best4cloud.user.domain.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.Predicate;
import javax.validation.constraints.NotEmpty;
import java.util.LinkedList;
import java.util.List;

@Data
@ApiModel(description = "系统用户登录视图")
public class SysLoginQuery {

    @NotEmpty(message = "用户名不能为空")
    @ApiModelProperty(value = "用户名", required = true)
    private String username;

    @NotEmpty(message = "密码不能为空")
    @ApiModelProperty(value = "密码", required = true)
    private String password;

    public Specification<SysUser> createSpec() {
        return (root, query, cb) -> {
            List<Predicate> predicates = new LinkedList<>();
            predicates.add(cb.equal(root.get("username"), username));
            predicates.add(cb.equal(root.get("password"), SecurityUtils.encodePassword(password)));
            return cb.and(predicates.toArray(Predicate[]::new));
        };
    }

}
